Fix new namespace creation placing files under absolute filesystem path#83
Conversation
When creating a new Clojure namespace from Project view, the new file could be created under a path that contains the absolute host path segments, e.g. creating src/proj/test.clj ended up at src/proj/home/user/code/proj/src/test.clj. Root cause: create-file-from-template built an absolute canonical path via io/file + getCanonicalPath and then tried to strip the directory prefix with string/replace-first. This is fragile: clojure-lsp may return source-paths as file:// URIs or with different separators than VirtualFile.getPath, causing the prefix strip to silently fail and leave the full absolute path as the new file name. IntelliJ then interprets it as a path relative to the target directory and recreates the whole host path tree inside it. Fix: rewrite path math using java.nio.file.Path. source-path detection normalizes URIs and separators, then relativize is used to compute the directory's path relative to the source root and the namespace path relative to the directory. No more string-prefix matching. Also cleans up dir->partial-namespace which used to silently produce garbage (home.user.code.proj...) when the source-path prefix did not match.
|
This failing test ( The "Publish Test Report" step also failed with Could a maintainer re-run the failed job or confirm whether |
| (str "."))))))) | ||
|
|
||
| (defn ^:private ns->rel-path | ||
| "Возвращает путь нового файла ОТНОСИТЕЛЬНО dir, как требует createFileFromTemplate." |
|
Could you add a CHANGELOG entry under |
|
The CI failure ( |
|
The
Could you merge |
The previous docstring was in Russian; translate to match the rest of the codebase. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…space-path # Conflicts: # CHANGELOG.md
|
Thank you! |
Bug
Creating a new Clojure namespace via Project view → New → Clojure namespace
sometimes creates the file under an absolute host path inside the target
directory.
Example: clicking "New namespace" on
src/project_name/and enteringtestcreates the file at:src/project_name/home/username/code/clojure_proj/project_name/src/test.clj
instead of the expected:
src/project_name/test.clj
Root cause
create-file-from-templateinnew_file.cljbuilt an absolute canonicalpath via
io/file+getCanonicalPathand then tried to strip thedirectory prefix with
string/replace-first.This is fragile:
clojure-lspmay returnsource-pathsasfile://URIsVirtualFile.getPathmay use a different separator thangetCanonicalPathsource-pathsmay be project-relative stringsWhen the prefix does not match,
replace-firstsilently returns the fullabsolute path, which IntelliJ then interprets as a path relative to the
target directory and recreates the whole host path tree inside it.
dir->partial-namespacehad the same class of bug — if the prefix didn'tmatch it returned
(subs filename 1)which produced nonsense namespaceslike
home.username.code.proj....Fix
Rewrite path math using
java.nio.file.Path:file://) and separators via a helper->pathPath.startsWithfor source-path detection (real segment-wisecomparison instead of substring matching)
Path.relativizeto computenamespace prefix in the dialog), and
new-namepassed to
CreateFileFromTemplateAction/createFileFromTemplate)No more string-prefix matching on raw paths.
Testing
Didn't rebuild locally (no toolchain on the machine I made the fix on).
Relying on CI + reviewer verification. Happy to iterate on review.